package interval;

import org.junit.Test;

import java.util.Arrays;

/*
 *@description 用最少数量的箭引爆气球
 *@author hkfan
 *@version 1.0
 *@create 2025/6/5 16:41
 */
public class LeetCode_452 {

    @Test
    public void test(){
        System.out.println(findMinArrowShots(new int[][]{
                new int[]{1,9},
                new int[]{7,16},
                new int[]{2,5},
                new int[]{7,12},
                new int[]{9,11},
                new int[]{2,10},
                new int[]{9,16},
                new int[]{3,9},
                new int[]{1,3}
        }));
    }

    public int findMinArrowShots(int[][] points) {
        //1. 排序
        Arrays.sort(points,(o1,o2)->{
            if (o1[0]>o2[0]){
                return 1;
            } else if (o1[0]<o2[0]) {
                return -1;
            }else {
                return (o1[1]>=o2[1])?1:-1;
            }
        });
        //2. 定义
        int res = 0;
        //3. 循环
        for (int i = 0; i < points.length; ) {
            int a=points[i][0],b=points[i][1];
            while (i+1< points.length && b>=points[i+1][0]){
                i++;a=Math.max(points[i-1][0],points[i][0]);b=Math.min(b,points[i][1]);
            }
            res++;i++;
        }
        return res;
    }



}
